package com.hu.jie.mywallpager.fragment;


import android.content.Context;
import android.content.Intent;
import android.nfc.Tag;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.toolbox.NetworkImageView;
import com.hu.jie.mywallpager.R;
import com.hu.jie.mywallpager.activity.HomeActivity;
import com.hu.jie.mywallpager.bean.GiftBean;
import com.hu.jie.mywallpager.utils.VolleyUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class HomeViewPagerFragment extends Fragment {
    private static final String URL= "parama";

    private ArrayList<String> mList;
    private GridView gv;
    private MyHomeAdapter adapter;

    private String mParama;

    public HomeViewPagerFragment() {

    }
    public static HomeViewPagerFragment newInstance(String parama){
        HomeViewPagerFragment fragment = new HomeViewPagerFragment();
        Bundle b = new Bundle();
        b.putString(URL,parama);
        Log.i("mParama","!!!!__________>>>>"+URL);
        fragment.setArguments(b);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments()!=null){
            mParama = getArguments().getString(URL);
            Log.i("mParama","!!!!__________>>>>"+mParama);
        }
        mList = new ArrayList<String>();

    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_home_view_pager, container, false);
    }
    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        gv = (GridView) view.findViewById(R.id.gv);
//        options = new DisplayImageOptions.Builder()
//                .bitmapConfig(Bitmap.Config.RGB_565).cacheInMemory(true)
//                .cacheOnDisk(true).showImageForEmptyUri(R.mipmap.ic_launcher)
//                .showImageOnFail(R.mipmap.ic_launcher)
//                .showImageOnLoading(R.mipmap.ic_launcher)
//                .build();


        adapter = new MyHomeAdapter();
        jsonInit();
        gv.setAdapter(adapter);

        gv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                startImagePagerActivity(position);
            }
        });

    }

    private void jsonInit() {
        VolleyUtils.newInstance(getContext()).newGsonRequest(Request.Method.GET, mParama, new Response.Listener<GiftBean>() {
            @Override
            public void onResponse(GiftBean response) {
                for (int i = 0; i < response.data.WallpaperListInfo.size(); i++) {
                    String wallPaperMiddle = response.data.WallpaperListInfo.get(i).WallPaperMiddle;
                    mList.add(wallPaperMiddle);
                    adapter.notifyDataSetChanged();
                }
            }
        },null,GiftBean.class);
    }

    private void startImagePagerActivity(int position) {
        listener(position);
    }

    private void listener(int position) {
        if (mList.size()!=0){
            Toast.makeText(getContext(), "点了"+position, Toast.LENGTH_SHORT).show();
        }else {
            Toast.makeText(getContext(), "无数据", Toast.LENGTH_SHORT).show();
        }
        Intent intent = new Intent(getContext(), HomeActivity.class);
        //intent.putExtra("url",position);
        intent.putStringArrayListExtra("mlist",mList);

        startActivity(intent);
    }

    class MyHomeAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            Log.i("TAG",mList.size()+"--------------------------");
            return mList.size();
        }
        @Override
        public Object getItem(int position) {
            return mList.get(position);
        }
        @Override
        public long getItemId(int position) {
            return position;
        }
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            LinearLayout ll = null;
            ViewHolder holder = null;
            if (convertView==null){
                holder = new ViewHolder();
                ll = (LinearLayout) getActivity().getLayoutInflater().inflate(R.layout.layout_home,parent,false);
                holder.niv = (NetworkImageView) ll.findViewById(R.id.network);
                ll.setTag(holder);
            }else {
                ll = (LinearLayout) convertView;
                holder = (ViewHolder) ll.getTag();
            }
            holder.niv.setImageUrl(mList.get(position), VolleyUtils.newInstance(getContext()).getLoader());
            //ImageLoader.getInstance().displayImage(mList.get(position),holder.niv,options);
            Log.i("TAG",""+mList.get(position));
            return ll;

        }
        private class ViewHolder {
            NetworkImageView niv;
        }
    }
}
